package interactive.focusPhoto.item 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import ginger.geom.RectangleShape;
	import interactive.focusPhoto.vo.PhotoVO;
	import caurina.transitions.Tweener;
	/**
	 * 图片子元素
	 * @author Ning
	 */
	public class PhotoItem extends Sprite
	{
		//是否用链接
		private var _isLink:Boolean;
		
		//载入完成后自动播放 运行play()
		private var _autoPlay:Boolean;
		
		//数据
		private var _data:PhotoVO
		//图片载入器
		protected var loader:Loader;
		//是否已载入
		private var _isLoad:Boolean;
		//展示的内容
		protected var content:DisplayObject;
		//内容容器
		protected var contentBox:Sprite;
		//遮罩
		protected var contentMask:RectangleShape;
		//是否有点击功能
		private var _mouseClickEnabled:Boolean;
		
		//宽
		private var _itemWidth:Number;
		//高
		private var _itemHeight:Number;
		
		public function PhotoItem() 
		{
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			
			contentBox = new Sprite();
			addChild(contentBox);
			
			contentMask = new RectangleShape();
			addChild(contentMask);
			
			contentBox.mask = contentMask;
			this.mouseClickEnabled = true;
			this.isLink = false;
			this.autoPlay = false;
			
		}
		/**
		 * 播放
		 */
		public function play():void
		{
			
		}
		/**
		 * 停止
		 */
		public function stop():void
		{
			
		}
		/**
		 * 设置元素大小
		 * @param	width
		 * @param	height
		 */
		public function setSize(width:Number, height:Number):void
		{
			this.itemWidth = width;
			this.itemHeight = height;
			setContentSize(width, height);
			
		}
		/**
		 * 设置主体内容的大小
		 * @param	width
		 * @param	height
		 */
		protected function setContentSize(width:Number, height:Number):void
		{
			contentMask.updateGraphics(width, height);
		}
		/**
		 * 载入
		 */
		public function load():void
		{
			if (_isLoad)
			    return;
				
            try {
			    loader.close();	
			}
			catch (e:Error) {
				
			}
			loadContent();
			_isLoad = true;
		}
		/**
		 * 加载的内容
		 */
		protected function loadContent():void
		{
			if(_data.IMG_URL!=null)
			    loader.load(new URLRequest(_data.IMG_URL), new LoaderContext(true));
			else 
			    onIoError();
		}
		/**
		 * 初始化
		 */
		public function init():void
		{
			_data = null;
			_isLoad = false;
		}
		/**
		 * 返回对象池时做的工作
		 */
		public function die():void
		{
            if (content is Bitmap)
			    Bitmap(content).bitmapData.dispose();
			else if (content is MovieClip)
			    MovieClip(content).stop();
			if (contentBox.contains(content))
			    contentBox.removeChild(content);
				
			content = null;
		}
		
		public function get data():PhotoVO 
		{
			return _data;
		}
		
		public function set data(value:PhotoVO):void 
		{
			_data = value;
		}
		
		public function get isLoad():Boolean 
		{
			return _isLoad;
		}
		
		public function set mouseClickEnabled(value:Boolean):void 
		{
			_mouseClickEnabled = value;
			if(_mouseClickEnabled&&!hasEventListener(MouseEvent.CLICK))
			    addEventListener(MouseEvent.CLICK, onClick);
			else {
				if (hasEventListener(MouseEvent.CLICK))
				    removeEventListener(MouseEvent.CLICK, onClick);
			}
		}
		
		public function get itemWidth():Number 
		{
			return _itemWidth;
		}
		
		public function set itemWidth(value:Number):void 
		{
			_itemWidth = value;
		}
		
		public function get itemHeight():Number 
		{
			return _itemHeight;
		}
		
		public function set itemHeight(value:Number):void 
		{
			_itemHeight = value;
		}
		
		public function get autoPlay():Boolean 
		{
			return _autoPlay;
		}
		
		public function set autoPlay(value:Boolean):void 
		{
			_autoPlay = value;
		}
		
		public function set isLink(value:Boolean):void 
		{
			_isLink = value;
		}
		
		public function get isLink():Boolean 
		{
			return _isLink;
		}
		
		/**
		 * 载入完成
		 * @param	evt
		 */
		protected function onComplete(evt:Event):void
		{
			content = evt.target.content as DisplayObject;
			if (content is Bitmap)
			    Bitmap(content).smoothing = true;
			contentBox.addChild(content);
			content.alpha = 0;
			Tweener.addTween(content, { time:.5, alpha:1 } );
			dispatchEvent(evt.clone());
		}
		
		/**
		 * 载入出错
		 * @param	evt
		 */
		protected function onIoError(evt:IOErrorEvent=null):void
		{
            dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
		}
		/**
		 * 点击事件
		 * @param	evt
		 */
		protected function onClick(evt:MouseEvent=null):void
		{
            link();
		}
		/**
		 * 链接 
		 */
		protected function link():void
		{
			if (ExternalInterface.available)
			    ExternalInterface.call('onFocusPhotoClick', data.ID);
				
			if (isLink) { 
			    navigateToURL(URLRequest(data.LINK), data.LINK_WINDOW);
			}
		}
	}

}